home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / RANDOM.ASM < prev    next >
Assembly Source File  |  1991-10-24  |  3KB  |  107 lines

  1. ;
  2. ; Random number generator.
  3. ; Original author: unknown.  This code was pulled off one of the nets and
  4. ;           added to the library.  Any information on the original
  5. ;           author would be appreciated.
  6. ;
  7. ;           Modified for use with UCR Standard library 10/24/91 rhyde
  8. ;
  9. ;
  10.  
  11. StdGrp        group    StdLib, StdData
  12.  
  13. StdData        segment    para public 'sldata'
  14. ;
  15. ; Note:  24 and 55 are not arbitrary.  They have been chosen so that the least
  16. ; significant bits in the sequence of pseudorandom integers have a period of
  17. ; length 2^55 - 1.  The sequence of pseudorandom numbers themselves have period
  18. ; 2^f*(2^55 - 1) where 0 <= f <= 16.  See Knuth's Volume 2 "Seminumerical
  19. ; Algorithms" of the second edition of the three volume set THE ART OF COMPUTER
  20. ; PROGRAMMING (pages 26 & 27).
  21.  
  22. j        dw    24 * 2        ; multiply by 2 for word offsets
  23. k        dw    55 * 2
  24.  
  25. ; Array of 55 seed elements for the additive pseudorandom number generator.
  26.  
  27. add_array    dw    ?    ; this location (offset 0 word) is not used
  28.         dw    7952,    42720,    56941,    47825,    52353,    4829,    32133
  29.         dw    29787,    7028,    62292,    46128,    34856,    63646,    21032
  30.         dw    62660,    61244,    35057,    36989,    43989,    46043,    48547
  31.         dw    43704,    29749,    21898,    10279,    48252,    35578,    27916
  32.         dw    3633,    50349,    33655,    36965,    48566,    43375,    15168
  33.         dw    30425,    8425,    31783,    3625,    23789,    37438,    64887
  34.         dw    19015,    43108,    61545,    24901,    58349,    52290,    62047
  35.         dw    21173,    27055,    27851,    47955,    14377,    14434
  36. StdData        ends
  37.  
  38. stdlib        segment    para public 'slcode'
  39.         assume    cs:StdGrp, ds:StdGrp
  40. ;
  41. ;
  42.         public    sl_randomize
  43. ;
  44. sl_randomize    proc    far        ; randomize the random number generator
  45.         push    ds
  46.         push    ax        ; save
  47.         push    bx
  48.         push    cx
  49. ;
  50.         mov    ax,40h        ; set ds to BIOS data area
  51.         mov    ds,ax
  52.         mov    bx,6ch        ; location of low word of 4-byte count
  53.         mov    ax,[bx]        ; get low word of 4-byte clock count
  54.         mov    bx, StdGrp    ; reset ds for code addressing
  55.         mov    ds, bx
  56.         mov    bx,offset add_array ; address array of seed elements
  57.         add    bx,2        ; offset 0 is not used
  58.         mov    cx,55        ; shall adjust all 55 seeds
  59. set_seed:    add    [bx],ax        ; randomize seed value with current time
  60.         add    bx,2        ; move to next one
  61.         loop    set_seed
  62. ;
  63.         pop    cx
  64.         pop    bx
  65.         pop    ax
  66.         pop    ds
  67.         ret
  68. sl_randomize    endp
  69. ;
  70. ;
  71. ;
  72. ; sl_Random-    Returns random number in AX (random bit values).
  73. ;
  74.         public    sl_Random
  75. ;
  76. sl_random    proc    far        ; generate pseudorandom number in ax
  77.         push    bx        ; save
  78.         push    cx
  79.         push    ds
  80. ;
  81.         mov    bx, StdGrp
  82.         mov    ds, bx
  83. ;
  84.         mov    bx,j        ; get j index
  85.         mov    cx,add_array[bx]; and load array element into cx
  86.         mov    bx,k        ; get k index
  87.         mov    ax,add_array[bx]; and load array element into ax
  88.         add    ax,cx        ; new element and return value to ax
  89.         mov    add_array[bx],ax; store new element at location k
  90.         sub    j,2        ; move down one element
  91.         sub    k,2        ; move down one element
  92.         cmp    j,0        ; is j down to 0?
  93.         jne    check_k        ; no, check k
  94.         mov    j,55 * 2    ; set i to end of array
  95. check_k:    cmp    k,0        ; is k down to 0?
  96.         jne    random_out    ; no, leave
  97.         mov    k,55 * 2    ; set k to end of array
  98. ;
  99. random_out:    pop    ds
  100.         pop    cx        ; restore
  101.         pop    bx
  102.         ret
  103. sl_random    endp
  104. ;
  105. StdLib        ends
  106.         end
  107.